//
//  LoadingView.m
//  Camera
//
//  Created by gml on 2018/11/13.
//  Copyright © 2018年 gml. All rights reserved.
//

#import "LoadingView.h"
@interface LoadingView()
{
    CALayer *_animationLayer;
    UIImageView *_imageView;
}
@end

static CGFloat lineWidth = 4.0f;
@implementation LoadingView
{
    CADisplayLink *_link;
    CAShapeLayer *_shapeLayer;
    CGFloat _startAngle;
    CGFloat _endAngle;
    CGFloat _progress;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self commonInit];
    }
    return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self commonInit];
    }
    return self;
}
- (void)commonInit
{
    self.backgroundColor = UIColorFromRGBA(0x000000, 0.5);
    self.userInteractionEnabled = NO;
    self.hidden = YES;

    //得到view的遮罩路径
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 75, 75)];
    bgView.center = CGPointMake(self.bounds.size.width * 0.5, self.bounds.size.height * 0.5);
    bgView.backgroundColor = UIColorFromRGBA(0x000000, 0.5);
    [self addSubview:bgView];
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:bgView.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight | UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(10,10)];
    UIColor *color = [UIColor redColor];
    [color set];
    [maskPath fill];
    //创建 layer
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = bgView.bounds;
    //赋值
    maskLayer.path = maskPath.CGPath;
    bgView.layer.mask = maskLayer;

    _animationLayer = [[CALayer alloc] init];
    _animationLayer.frame = bgView.layer.bounds;
    [bgView.layer addSublayer:_animationLayer];
    
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *resourcePath = [bundle resourcePath];
    NSString *filePath = [resourcePath stringByAppendingPathComponent:@"loading@2x.png"];
    _imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:filePath]]; //[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"loading_00000"]];
    _imageView.center = CGPointMake(bgView.bounds.size.width*0.5, bgView.bounds.size.height*0.5);
    [bgView addSubview:_imageView];
    
    [bgView setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [bgView setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
    
}
- (void)startAnimating {
    if (!_animationLayer.sublayers) {
        [self setupAnimation];
    }
    self.hidden = NO;
    _animationLayer.speed = 1.0f;
    _animating = YES;
}

- (void)stopAnimating {
    _animationLayer.speed = 0.0f;
    _animating = NO;
    self.hidden = YES;
}

- (void)setupAnimation {
    _animationLayer.sublayers = nil;
    [self setupAnimationInLayer:_animationLayer withSize:CGSizeMake(_animationLayer.bounds.size.width *0.8, _animationLayer.bounds.size.width *0.8) tintColor:[UIColor whiteColor]];
    
    CABasicAnimation *animation = [CABasicAnimation animation];
    animation.keyPath = @"transform.scale";
    animation.toValue = @(1);
    animation.fromValue = @(0);
//    animation.autoreverses = YES;
    animation.duration = 1.0;
    animation.fillMode = kCAFillModeForwards;
//    animation.repeatCount = MAXFLOAT;
//    [_imageView.layer addAnimation:animation forKey:nil];
    
}
- (void)setupAnimationInLayer:(CALayer *)layer withSize:(CGSize)size tintColor:(UIColor *)tintColor {
    CGFloat oX = (layer.bounds.size.width-size.width)*0.5;
    CGFloat oY = (layer.bounds.size.height-size.height)*0.5;
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.frame = CGRectMake(oX, oY, size.width, size.height);
    shapeLayer.fillColor = [UIColor clearColor].CGColor;
    shapeLayer.strokeColor = tintColor.CGColor;
    shapeLayer.lineWidth = lineWidth;
    shapeLayer.lineCap = kCALineCapRound;
    [layer addSublayer:shapeLayer];
    _shapeLayer = shapeLayer;
    
    _link = [CADisplayLink displayLinkWithTarget:self selector:@selector(displayLinkAction)];
    [_link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
    _link.paused = true;
    [_link setPaused:NO];
    
}
- (void)displayLinkAction {
    _progress += [self speed];
    if (_progress >= 1) {
        _progress = 0;
    }
    [self updateAnimationLayer];
}

- (void)updateAnimationLayer {
    _startAngle = -M_PI_2;
    _endAngle = -M_PI_2 +_progress * M_PI * 2;
    if (_endAngle > M_PI) {
        CGFloat progress1 = 1 - (1 - _progress)/0.25;
        _startAngle = -M_PI_2 + progress1 * M_PI * 2;
    }
    CGFloat radius = _shapeLayer.bounds.size.width/2.0f - lineWidth/2.0f;
    CGFloat centerX = _shapeLayer.bounds.size.width/2.0f;
    CGFloat centerY = _shapeLayer.bounds.size.height/2.0f;
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(centerX, centerY) radius:radius startAngle:_startAngle endAngle:_endAngle clockwise:true];
    path.lineCapStyle = kCGLineCapRound;
    _shapeLayer.path = path.CGPath;
    
}

- (CGFloat)speed {
    if (_endAngle > M_PI) {
        return 0.3/60.0f;
    }
    return 2/60.0f;
}


@end
